﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Battlefield2
    {
        public Battlefield2()
        {
            int testCount = Int32.Parse(Console.ReadLine());

            for (int testCase = 1; testCase <= testCount; testCase++)
            {
                string[] input = Console.ReadLine().Split(' ');
                long totalCount = long.Parse(input[0]);
                long[] count = new long[7];
                for (int i = 1; i < 7; i++)
                {
                    count[i] = long.Parse(input[i]);
                }
                int[,] board = new int[4, 4];
                for (int x = 1; x < 4; x++)
                {
                    input = Console.ReadLine().Split(' ');
                    for (int y = 1; y < 4; y++)
                    {
                        board[x, y] = Int32.Parse(input[y - 1]);
                    }
                }
                List<int[]>[] order = new List<int[]>[7];
                for (int i = 1; i < 4; i++)
                {
                    order[i] = new List<int[]>();
                    for (int j = 1; j < 4; j++)
                    {
                        order[i].Add(new int[2] { j + 3, board[i, j] });
                    }
                    order[i] = order[i].OrderBy(s => s[1]).Reverse().ToList();
                }
                for (int i = 1; i < 4; i++)
                {
                    order[i + 3] = new List<int[]>();
                    for (int j = 1; j < 4; j++)
                    {
                        order[i + 3].Add(new int[2] { j, board[j, i] });
                    }
                    order[i + 3] = order[i + 3].OrderBy(s => s[1]).Reverse().ToList();
                }
                long restCount = totalCount;
                long sum = 0;

                while (restCount > 0)
                {
                    int[] enermy = new int[7];
                    CheckCount(order, count);
                    FindEnermy(order, enermy);
                    for (int i = 1; i <= 3; i++)
                    {
                        int a = i;
                        int b = enermy[i];
                        if (b < Int32.MaxValue)
                        {
                            long people = Math.Min(count[a], count[b]);
                            count[a] -= people;
                            count[b] -= people;
                            restCount -= people;
                            sum += board[a, b - 3] * people;
                        }
                    }

                }

                Console.WriteLine("Case #" + testCase + ": " + sum);
            }
        }

        private void CheckCount(List<int[]>[] order, long[] count)
        {
            for (int i = 1; i < 7; i++)
            {
                order[i].RemoveAll(m => count[m[0]] == 0);
            }
            for (int i = 1; i < 7; i++)
            {
                if (count[i] == 0)
                {
                    order[i].Clear();
                }
            }
        }

        private void FindEnermy(List<int[]>[] order, int[] enermy)
        {
            enermy[4] = -1;
            enermy[5] = -1;
            enermy[6] = -1;
            while (enermy.Count(b => b != 0) < 6)
            {
                for (int i = 1; i <= 3; i++)
                {
                    if (enermy[i] == 0)//1~3중에서 적이 아직 없는 경우
                    {
                        if (order[i].Count == 0) //싸울 사람이 없음
                        {
                            enermy[i] = int.MaxValue;
                            continue;
                        }
                        for (int j = 0; j < order[i].Count; j++)
                        {
                            int myEnermy = order[i][j][0]; //적 후보
                            if (enermy[myEnermy] > 0) //그 후보가 적이 있는 경우
                            {
                                int enermyOfEnermy = enermy[myEnermy]; //적의 적
                                int myIndex = order[myEnermy].FindIndex(e => e[0] == i);
                                int otherIndex = order[myEnermy].FindIndex(e => e[0] == enermyOfEnermy);
                                if (myIndex < otherIndex) //상대 빼앗기
                                {
                                    enermy[i] = myEnermy;
                                    enermy[myEnermy] = i;
                                    enermy[enermyOfEnermy] = 0;
                                    break;
                                }                                
                            }
                            else //적이 없는 경우
                            {
                                enermy[myEnermy] = i;
                                enermy[i] = myEnermy;
                                break;
                            }
                        }
                        //상대할 적을 못찾은 경우
                        if (enermy[i] == 0)
                        {
                            enermy[i] = int.MaxValue;
                        }
                    }
                }
            }
        }
    }
}
